package com.beiben.workflow.service.impl;

import com.beiben.workflow.domain.WfCheck;
import com.beiben.workflow.domain.WfCheckCountVo;
import com.beiben.workflow.domain.WfCheckLog;
import com.beiben.workflow.domain.WfCheckVo;
import com.beiben.workflow.mapper.WfCheckMapper;
import com.beiben.workflow.service.IWfCheckService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审批记录Service业务层处理
 *
 * @author 熊国强
 * @date 2020-12-17
 */
@Service
public class WfCheckServiceImpl implements IWfCheckService {


    @Autowired
    private WfCheckMapper wfCheckMapper;

    /**
     * 查询审批记录
     *
     * @param checkId 审批记录ID
     * @return 审批记录
     */
    @Override
    public WfCheck selectWfCheckById(Long checkId) {
        return wfCheckMapper.selectWfCheckById(checkId);
    }

    /**
     * 查询审批记录列表
     *
     * @param wfCheck 审批记录
     * @return 审批记录
     */
    @Override
    public List<WfCheck> selectWfCheckList(WfCheck wfCheck) {
        return wfCheckMapper.selectWfCheckList(wfCheck);
    }

    /**
     * 新增审批记录
     *
     * @param wfCheck 审批记录
     * @return 结果
     */
    @Override
    public int insertWfCheck(WfCheck wfCheck) {
        wfCheck.setCreateTime(DateUtils.getNowDate());
        return wfCheckMapper.insertWfCheck(wfCheck);
    }

    /**
     * 修改审批记录
     *
     * @param wfCheck 审批记录
     * @return 结果
     */
    @Override
    public int updateWfCheck(WfCheck wfCheck) {
        return wfCheckMapper.updateWfCheck(wfCheck);
    }

    /**
     * 批量删除审批记录
     *
     * @param checkIds 需要删除的审批记录ID
     * @return 结果
     */
    @Override
    public int deleteWfCheckByIds(Long[] checkIds) {
        return wfCheckMapper.deleteWfCheckByIds(checkIds);
    }

    /**
     * 删除审批记录信息
     *
     * @param checkId 审批记录ID
     * @return 结果
     */
    @Override
    public int deleteWfCheckById(Long checkId) {
        return wfCheckMapper.deleteWfCheckById(checkId);
    }

    /**
     * 我的审批
     *
     * @param wfCheckVo
     * @return
     */
    @Override
    @DataScope(userAlias = "u")
    public List<WfCheckVo> approveList(WfCheckVo wfCheckVo) {
        wfCheckVo.setCheckUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        if("1".equals(wfCheckVo.getApplyType())){//项目工作
            return wfCheckMapper.selectWfCheckProjectByUserId(wfCheckVo);
        }else if("2".equals(wfCheckVo.getApplyType())){
            return wfCheckMapper.selectWfCheckProjectDistByUserId(wfCheckVo);
        }else {//日常工作
            return wfCheckMapper.selectWfCheckWorkByUserId(wfCheckVo);
        }
//        return wfCheckMapper.selectWfCheckByUserId(wfCheckVo);
    }

    /**
     * 根据审批流程id 修改流程状态
     *
     * @param wfCheckLog
     * @return
     */
    @Override
    public int updateWfCheckState(WfCheckLog wfCheckLog) {
        WfCheck wfCheck = new WfCheck();
        wfCheck.setCheckTime(wfCheckLog.getCreateTime());
        wfCheck.setState(wfCheckLog.getState());
        wfCheck.setCheckId(wfCheckLog.getCheckId());
        return wfCheckMapper.updateWfCheckState(wfCheck);
    }

    /**
     * 统计审核数据
     *
     * @param
     * @return 统计记录
     */
    @Override
//    @DataScope(userAlias = "u")
    public WfCheckCountVo selectWfCheckCount() {
        WfCheckVo wfCheckVo = new WfCheckVo();
        wfCheckVo.setCheckUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return wfCheckMapper.selectWfCheckCount(wfCheckVo);
    }

}
